﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Views;
using CommunitySystem.SystemService;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.IO;
using CommunitySystem.Infrastructure.CommonMethod;


namespace CommunitySystem
{
    public partial class SystemManagerControl : UserControl, IAsyncCtrl
    {
        public NavMoveSwitch Nav { get; set; }
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private DataService.DataServiceClient client = null;
        public string Title
        {
            get { return ""; }
            set { Title = value; }
        }

        public bool PageLoaded
        {
            get;
            set;
        }
        private int _pid = 0;

        private BusyIndicator _busyIndicator;
        public SystemManagerControl(int pid, BusyIndicator busyIndicator)
        {
            InitializeComponent();
            _pid = pid;
            _busyIndicator = busyIndicator;
            client = new DataService.DataServiceClient();
            LoadMenuDynamic lm = new LoadMenuDynamic(tabControl1);
            lm.LoadMenu(client, _pid, myTree);
            //App.currentTabControl = tabControl1;
            //client.GetMenuByPIDAsync(pid);
            //client.GetMenuByPIDCompleted += new EventHandler<DataService.GetMenuByPIDCompletedEventArgs>(client_GetMenuByPIDCompleted);
        }

        void client_GetMenuByPIDCompleted(object sender, DataService.GetMenuByPIDCompletedEventArgs e)
        {
           // var menu = e.Result;
            ObservableCollection<CommunitySystem.DataService.MenuItem> items = e.Result;
            foreach (var i in items.Where(c => c.PID == _pid))
            {
                TreeViewItem item = new TreeViewItem();
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title")});
                StackPanel sp = new StackPanel() { Cursor = Cursors.Hand, Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.icon != null && i.icon.Length > 0)
                {
                    Image img = new Image() { Cursor=Cursors.Hand, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, Width = 16, Height = 16, Stretch = Stretch.Fill, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.icon, 0, i.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(i.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Cursor = Cursors.Hand, Text = i.Title, FontSize = 12, Foreground = new SolidColorBrush(Colors.Black), Margin = new Thickness(4, 0, 0, 0) });
                item.Header = sp;
                sp.DataContext = i;
                item.DataContext = i;
                AddChildNode(item, i, items);
                sp.MouseLeftButtonDown += new MouseButtonEventHandler(sp_MouseLeftButtonDown);
                //item.MouseLeftButtonDown += new MouseButtonEventHandler(sp_MouseLeftButtonDown);
                myTree.Items.Add(item);
            }
            CDGL_Click(null, null);
            _busyIndicator.IsBusy = false;
        }

        void AddChildNode(TreeViewItem pitem, CommunitySystem.DataService.MenuItem pmenu, ObservableCollection<CommunitySystem.DataService.MenuItem> source)
        {
            foreach (var i in source.Where(c => c.PID == pmenu.ID))
            {
                TreeViewItem item = new TreeViewItem();
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title")});
                StackPanel sp = new StackPanel() { Cursor = Cursors.Hand, Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.icon != null && i.icon.Length > 0)
                {
                    Image img = new Image() { Cursor = Cursors.Hand, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, Width = 16, Height = 16, Stretch = Stretch.Fill, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.icon, 0, i.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(i.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Cursor = Cursors.Hand, Text = i.Title, FontSize = 12, Foreground = new SolidColorBrush(Colors.Black), Margin = new Thickness(4, 0, 0, 0) });
                item.Header = sp;
                sp.DataContext = i;
                item.DataContext = i;
                AddChildNode(item, i, source);
                sp.MouseLeftButtonDown += new MouseButtonEventHandler(sp_MouseLeftButtonDown);
                //item.MouseLeftButtonDown += new MouseButtonEventHandler(sp_MouseLeftButtonDown);
                pitem.Items.Add(item);
            }
        }

        void sp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel sp = sender as StackPanel;
            DataService.MenuItem item = sp.DataContext as DataService.MenuItem;
            if (item != null)
            {
                switch (item.Title)
                {
                    case "菜单管理":
                        CDGL_Click(null, null);
                        break;
                    case "数据字典":
                        SJZD_Click(null, null);
                        break;
                    case "村居委管理":
                        CJW_Click(null, null);
                        break;
                    case  "用户管理":
                        YHGL_Click(null, null);
                        break;
                    case "组织机构管理":
                        ZZJGGL_Click(null, null);
                        break;
                    case "角色管理":
                        JSGL_Click(null, null);
                        break;
                }
            }
        }

        

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //orgListView.Nav = Nav;
        }

        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }

        /// <summary>
        /// 主题配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZTPZ_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TJPZ_Click(object sender, RoutedEventArgs e)
        {

        }

        private void YWPZ_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 权限配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QXPZ_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 村居委管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CJW_Click(object sender, RoutedEventArgs e)
        {
            //this.AddTabItem(new CJWListControl(), "村居委管理", "CJWManager", tabControl1);
        }
        /// <summary>
        /// 菜单管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CDGL_Click(object sender, RoutedEventArgs e)
        {
            this.AddTabItem(new MenuManage(), "菜单管理", "MenuManager", tabControl1);
        }

        void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RemoveTabItem();
        }

        public void RemoveTabItem()
        {
            tabControl1.Items.Remove(tabControl1.SelectedItem);
        }

        /// <summary>
        /// 数据字典
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SJZD_Click(object sender, RoutedEventArgs e)
        {
            //dataAreaGrid.Children.Clear();
            //DataDictionaryControl DDC = new DataDictionaryControl();

            //dataAreaGrid.Children.Add(DDC);
            AddTabItem(new DataDictionaryControl(), "数据字典", "DataDict", tabControl1);
        }
        /// <summary>
        /// 用户管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void YHGL_Click(object sender, RoutedEventArgs e)
        {
            AddTabItem(new UserManagerControl(), "用户管理", "UserManager", tabControl1);
        }

        /// <summary>
        /// 组织机构管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZZJGGL_Click(object sender, RoutedEventArgs e)
        {
            AddTabItem(new OrgControl(), "组织机构管理", "OrgManager", tabControl1);
        }

        /// <summary>
        /// 角色管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void JSGL_Click(object sender, RoutedEventArgs e)
        {
            AddTabItem(new RoleManagerControl(), "角色管理", "RoleManager", tabControl1);
        }

        /// <summary>
        /// 每次如果有相同的tabid， 则不新增tab页，
        /// </summary>
        /// <param name="uc"></param>
        /// <param name="header"></param>
        /// <param name="tabid"></param>
        void AddTabItem(UserControl uc, string header, string tabid,TabControl Control)
        {
            CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.addNewTabItem(uc, header, tabid, Control, App.Current.Resources["tabItemStyle"] as Style);
           
        }

       
    }

    
}
